/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.developer.spoti.vspot.utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentState;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * CustomText
 *
 * @author AnBetter
 * @since 2021-06-22
 */
public class CustomText extends Text {
    private final Context mContext;

    private float cornerRadius = 0f;

    private float cornerRadiusLeftTop = 0f;

    private float cornerRadiusLeftBottom = 0f;

    private float cornerRadiusRightTop = 0f;

    private float cornerRadiusRightBottom = 0f;

    private Color bgDefaultColor = Color.TRANSPARENT;

    private Color bgPressedColor = Color.TRANSPARENT;

    private int strokeWidth = 0;

    private Color strokeColor = Color.TRANSPARENT;

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     */
    public CustomText(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public CustomText(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        mContext = context;

        if (attrSet.getAttr("cornerRadius").isPresent()) {
            cornerRadius = attrSet.getAttr("cornerRadius").get().getFloatValue();
        }
        if (attrSet.getAttr("cornerRadiusLeftTop").isPresent()) {
            cornerRadiusLeftTop = attrSet.getAttr("cornerRadiusLeftTop").get().getFloatValue();
        }
        if (attrSet.getAttr("cornerRadiusLeftBottom").isPresent()) {
            cornerRadiusLeftBottom = attrSet.getAttr("cornerRadiusLeftBottom").get().getFloatValue();
        }
        if (attrSet.getAttr("cornerRadiusRightTop").isPresent()) {
            cornerRadiusRightTop = attrSet.getAttr("cornerRadiusRightTop").get().getFloatValue();
        }
        if (attrSet.getAttr("cornerRadiusRightBottom").isPresent()) {
            cornerRadiusRightBottom = attrSet.getAttr("cornerRadiusRightBottom").get().getFloatValue();
        }
        if (attrSet.getAttr("bgDefaultColor").isPresent()) {
            bgDefaultColor = attrSet.getAttr("bgDefaultColor").get().getColorValue();
        }
        if (attrSet.getAttr("bgPressedColor").isPresent()) {
            bgPressedColor = attrSet.getAttr("bgPressedColor").get().getColorValue();
        }
        if (attrSet.getAttr("strokeWidth").isPresent()) {
            strokeWidth = attrSet.getAttr("strokeWidth").get().getIntegerValue();
        }
        if (attrSet.getAttr("strokeColor").isPresent()) {
            strokeColor = attrSet.getAttr("strokeColor").get().getColorValue();
        }

        init();
    }

    private void init() {
        StateElement stateElement = new StateElement();
        if (bgDefaultColor.getValue() != Color.TRANSPARENT.getValue()) {
            ShapeElement seDefault = getShapeElement();
            seDefault.setRgbColor(RgbColor.fromArgbInt(bgDefaultColor.getValue()));
            stateElement.addState(new int[]{-ComponentState.COMPONENT_STATE_PRESSED}, seDefault);
        }
        if (bgPressedColor.getValue() != Color.TRANSPARENT.getValue()) {
            ShapeElement sePressed = getShapeElement();
            sePressed.setRgbColor(RgbColor.fromArgbInt(bgPressedColor.getValue()));
            stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, sePressed);
        }
        setBackground(stateElement);
    }

    /**
     * 获取公共的Element，一般不同状态下圆角都一样。
     *
     * @return ShapeElement
     */
    private ShapeElement getShapeElement() {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.RECTANGLE);
        if (cornerRadius != 0f) {
            shapeElement.setCornerRadius(vp2px(cornerRadius));
        } else {
            shapeElement.setCornerRadiiArray(new float[]{
                    vp2px(cornerRadiusLeftTop), vp2px(cornerRadiusLeftTop),
                    vp2px(cornerRadiusRightTop), vp2px(cornerRadiusRightTop),
                    vp2px(cornerRadiusRightBottom), vp2px(cornerRadiusRightBottom),
                    vp2px(cornerRadiusLeftBottom), vp2px(cornerRadiusLeftBottom),
            });
        }
        if (strokeWidth != 0f) {
            shapeElement.setStroke((int) vp2px(strokeWidth), RgbColor.fromArgbInt(strokeColor.getValue()));
        }
        return shapeElement;
    }

    /**
     * 单位转换
     *
     * @param value
     * @return float
     */
    private float vp2px(float value) {
        return AttrHelper.vp2px(value, mContext);
    }
}
